using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;

namespace FluidDemo.Model
{
    public static class CollitionResolver
    {
        public static bool Solve(Particle a, Particle b)
        {
            Vector2 x1 = a.Position;
            Vector2 x2 = b.Position;
            Vector2 delta = x2 - x1;
            float deltalength = delta.X * delta.X + delta.Y * delta.Y;
            float ra = a.Radius, rb = b.Radius, rab = ra + rb, rab2 = rab * rab;
            if (deltalength > rab2)
                return false;
            deltalength = (float)Math.Sqrt(deltalength);
            float diff = (deltalength - (rab)) / (deltalength);
            if (float.IsInfinity(diff)) 
                return false;

            float ms = a.Mass + b.Mass;
            float ma = a.Mass / (ms);
            float mb = b.Mass / (ms);
            
            // ---
            //float ma = 0.5f, mb = 0.5f;

            //ma *= 0.85f;
            //mb *= 0.85f;


            x1 += delta * ma * diff;
            x2 -= delta * mb * diff;

            a.Position = x1;
            b.Position = x2;

            //a.Energy -= 0.05f;
            //b.Energy -= 0.05f;

            return true;
        }
    }
}
